package com.reliable.api.dto.excel;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import com.reliable.api.dto.ip.taskInfo.HistoricalTasks;
import com.reliable.api.dto.ip.taskInfo.XssIpLocationDto;
import lombok.Data;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 域名信息导出专用 DTO
 * 将复杂的 DomainDto 扁平化，以便 EasyExcel 导出。
 */
@Data
@HeadRowHeight(20)
@ContentRowHeight(18)
public class DomainExcel {

    @ExcelProperty("域名")
    @ColumnWidth(30)
    private String domain;

    @ExcelProperty("注册商")
    @ColumnWidth(20)
    private String registrar;

    @ExcelProperty("注册时间")
    @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
    @ColumnWidth(20)
    private Date createTime;

    @ExcelProperty("更新时间")
    @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
    @ColumnWidth(20)
    private Date updateTime;

    @ExcelProperty("过期时间")
    @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
    @ColumnWidth(20)
    private Date expireTime;

    @ExcelProperty("网站标题")
    @ColumnWidth(30)
    private String websiteTitle;

    @ExcelProperty("关联任务数")
    @ColumnWidth(10)
    private Integer associatedTaskCount;

    @ExcelProperty("关联终端数")
    @ColumnWidth(10)
    private Integer associatedTerminalCount;

    @ExcelProperty("首次捕获")
    @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
    @ColumnWidth(20)
    private Date firstCaptured;

    @ExcelProperty("最后捕获")
    @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
    @ColumnWidth(20)
    private Date lastCaptured;

    @ExcelProperty("DNS服务器")
    @ColumnWidth(40)
    private String dnsServeString; // 将List<String>转换为字符串

    @ExcelProperty("总访问次数")
    @ColumnWidth(10)
    private Long totalVisits;

    @ExcelProperty("SSL信息")
    @ColumnWidth(30)
    private String ssl;

    @ExcelProperty("IP列表")
    @ColumnWidth(50)
    private String ipListString; // 将Map<String, List<XssIpLocationDto>>转换为字符串

    @ExcelProperty("历史任务")
    @ColumnWidth(60)
    private String historicalTasksString; // 将List<HistoricalTasks>转换为字符串

    // --- 以下是 transient 字段，EasyExcel 会忽略它们，仅用于内部计算 ---

    private transient List<String> DnsServe;
    private transient Map<String, List<XssIpLocationDto>> ipList;
    private transient List<HistoricalTasks> historicalTasks;

    // --- 自定义 Getter 方法，用于动态生成导出内容 ---

    public String getDnsServeString() {
        if (this.DnsServe == null || this.DnsServe.isEmpty()) {
            return "";
        }
        return String.join(", ", this.DnsServe);
    }

    public String getIpListString() {
        if (this.ipList == null || this.ipList.isEmpty()) {
            return "";
        }
        return this.ipList.entrySet().stream()
                .map(entry -> entry.getKey() + ": " + entry.getValue().stream()
                        .map(this::formatIpLocation)
                        .collect(Collectors.joining("; ")))
                .collect(Collectors.joining(" | "));
    }

    private String formatIpLocation(XssIpLocationDto dto) {
        if (dto == null) return "";
        return String.format("IP:%s, 位置:%s", dto.getIp(), dto.getIpLocation());
    }

    public String getHistoricalTasksString() {
        if (this.historicalTasks == null || this.historicalTasks.isEmpty()) {
            return "";
        }
        return this.historicalTasks.stream()
                .map(this::formatHistoricalTask)
                .collect(Collectors.joining("; "));
    }

    /**
     * 格式化 HistoricalTasks 对象，只包含 ipOrDomain 和 taskName 两个字段。
     * @param task HistoricalTasks 对象
     * @return 格式化后的字符串
     */
    private String formatHistoricalTask(HistoricalTasks task) {
        if (task == null) return "";
        return String.format("域名/IP:%s, 任务名:%s",
                task.getIpOrDomain() != null ? task.getIpOrDomain() : "",
                task.getTaskName() != null ? task.getTaskName() : "");
    }
}